package com.solocredit.finance.ui.user.impl

import android.content.Context
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.ApiService.addRandomParams
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.user.view.IMullaIUserPointsView
import com.solocredit.finance.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/12 16:26
 * @Annotation：用户积分明细列表 Presenter 实现
 */
class MullaUserPointsPresenterImpl(context: Context?) : MvpPresenter<IMullaIUserPointsView>(context) {

    /**
     * 获取积分明细列表
     */
    fun loadMullaUserPointsListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_COUPON_LIST_PATH)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
//            put("product",mvpView?.loadProductName())
//            put("token",mvpView?.loadProductToken())
            put("mobile", SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_MOBILE))
            put("infoType","scoreHistory")
            put("page",mvpView?.loadMullaPage())
            put("size","20")
        }
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                mvpView?.hideMullaLoading()
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null){
                            val pointsArray = obj.optJSONArray("userScoreLogList")
                            val pointsList = arrayListOf<JSONObject>()
                            if (pointsArray != null) {
                                for (i in 0..<pointsArray.length()){
                                    pointsList.add(pointsArray.optJSONObject(i))
                                }
                            }
                            val totalCount = obj.optInt("userScoreLogSize")
                            mvpView?.showMullaUserPointsList(totalCount,pointsList)
                        }
                    }
                    else -> {
                        mvpView?.showMullaToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }
}